package code.ws_common_scene.eft
{
	import code.ws_common_scene.texturedata.EftTextureDataPool;
	import code.ws_common_scene.texturedata.TexturePlayData;
	import code.ws_common_scene.texturedata.Textures;
	import code.ws_common_utils.UrlUtils;
	
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.utils.getDefinitionByName;
	
	import je.engine.loaderware.realtimeLoa.seq.JEAutoLoaSwf;
	
	import starling.display.Image;
	import starling.textures.Texture;
	import starling.utils.Color;

	/**
	 * 特效刷新器 
	 * @author qiangchen
	 * 
	 */	
	public class EViewClip extends Image
	{
		/** 皮肤样式 */
		private var _style:String = null;
		/** 动作样式加载器 */
		private var _loaStyle:JEAutoLoaSwf = null;
		/** 动作播放数据 */
		private var _playData:TexturePlayData = null;
		/** 当前帧 */
		public var _crtFrame:int = 0;
		/** 播放速率控制器 */
		public var _fr:int;
		/** 是否正在加载swf资源 */
		private var _isLoading:Boolean = false;
		
		
		
		/**
		 * 构造函数 
		 * 
		 */		
		public function EViewClip()
		{
			super( Texture.fromBitmapData( new BitmapData(1,1,false,Color.BLACK), false ) );
			this.touchable = false;
		}
		
		
		/**
		 * 初始化 
		 * @param style
		 * 
		 */		
		public function ini( style:String ):void
		{
			_style = style;
			_playData = EftTextureDataPool.getPlayData( style );
			if( !_playData )
			{
				throw new Error("[EViewClip]---> It's Error! TexturePlayData is null. style: " + style);
				return;
			}
			
			_isLoading = false;
		}
		
		
		
		/**
		 * 刷新 
		 * 
		 */		
		public function refresh():void
		{
			_fr ++;
			if( _fr >= _playData.frameRate )
			{
				_fr = 0;
				if( _crtFrame > _playData.textures.length -1 )
				{
					_crtFrame = 0;
				}
				
				var t:Textures = _playData.textures[_crtFrame];
				if( !t )
				{
					trace("EViewClip ---> play data is null.");
					return;
				}
				
				
				try
				{
					var _class:Class = getDefinitionByName( t.key ) as Class;
					var bmp:BitmapData = new _class() as BitmapData;
					
					this.texture = Texture.fromBitmapData( bmp, false );
					this.readjustSize();
					
					this.scaleX = t.scal;
					this.x = t.offx;
					this.y = t.offy;
					
					bmp.dispose();
					bmp = null;
					_class = null;
				}
				catch( e:Error )
				{
					if( !_isLoading )
					{
						_loaStyle = new JEAutoLoaSwf( loaCom, null, "", false);
						_loaStyle.startStram( UrlUtils.getEftUrl(  _playData.style  ) );
						
						_isLoading = true;
					}
				}
				
				_crtFrame ++;
			}
		}
		
		
		/**
		 * 动作播放数据器加载完成 
		 * @param mc
		 * 
		 */		
		private function loaCom( mc:MovieClip ):void
		{
			mc = null;
			cancelLoa();
			
			_isLoading = false;
		}
		
		/**
		 * 取消加载 
		 * 
		 */		
		public function cancelLoa():void
		{
			if( _loaStyle )
			{
				_loaStyle.autoRelease();
				_loaStyle = null;
			}
		}
		
		/**
		 * 获取皮肤样式 
		 * @return 
		 * 
		 */		
		public function get style():String
		{
			return _style;
		}
		
		
		/**
		 * 获取播放数据 
		 * @return 
		 * 
		 */		
		public function get playData():TexturePlayData
		{
			return _playData;
		}
		
		
		/**
		 * 释放 
		 * 
		 */		
		public function die():void
		{
			cancelLoa();
			_playData = null;
			_crtFrame = -1;
			_fr = -1;
			_style = null;
		}
	}
}